﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class InvoiceRepository : RepositoryBase<Invoice>
    {
        public IQueryable<InvoiceView> GetInvoiceListGroupByOrder(string orderNumber)//, DateTime beginTime, DateTime endTime
        {
            var result = from invo in dbcontext.Invoices
                         //where invo.Status == 2
                         group invo by invo.OrderId into tempInvo
                         join order in dbcontext.Orders
                         on tempInvo.Key equals order.Id
                         join customer in dbcontext.Customers
                         on order.CustomeId equals customer.Id
                         where (string.IsNullOrEmpty(orderNumber) || order.CusNumber.Contains(orderNumber)) && order.Status == 11 && order.Stage != -10
                         //&& (order.CreateTime >= beginTime && order.CreateTime <= endTime)
                         select new InvoiceView
                         {
                             OrderId = order.Id,
                             OrderStatus = order.Status,
                             OrderNumber = order.Number,
                             CreateTime = order.CreateTime,
                             CreatorName = customer.Name,
                             CusNumber = order.CusNumber
                         };

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="number">发货单号</param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public IQueryable<InvoiceView> SendCarQuery(int type, string number, DateTime beginTime, DateTime endTime)
        {
            var result = from invo in dbcontext.Invoices.Where(a => a.Type == 1 && a.Status != -1)
                         join sendCar in dbcontext.SendCars
                         on invo.Id equals sendCar.InvoicId
                         where (invo.Number.Contains(number)) && invo.ApplyTime <= endTime && invo.ApplyTime >= beginTime
                         select new InvoiceView
                         {
                             Id = invo.Id,
                             SubId = sendCar.Id,
                             Status = sendCar.Status,
                             CreatorName = invo.CreatorName,
                             ApplyTime = invo.ApplyTime,
                             DliveryDemand = sendCar.Demand,
                             Number = invo.Number,
                             Worth = sendCar.Worth,
                             TakeCompany = invo.TakeCompany,
                             SendType = sendCar.SendType,
                             SendCardType = sendCar.Type
                         };
            var result1 = from sendCar in dbcontext.SendCars.Where(a => a.Type == 1)
                         where (sendCar.Number.Contains(number)) && sendCar.CreateTime <= endTime && sendCar.CreateTime >= beginTime
                         select new InvoiceView
                         {
                             Id = sendCar.Id,
                             SubId = sendCar.Id,
                             Status = sendCar.Status,
                             CreatorName = sendCar.CreatorName,
                             ApplyTime = sendCar.CreateTime.Value,
                             DliveryDemand = sendCar.Demand,
                             Number = sendCar.Number,
                             Worth = sendCar.Worth,
                             TakeCompany = sendCar.TakeCompany,
                             SendType = sendCar.SendType,
                             SendCardType = sendCar.Type
                         };
            return result.Concat(result1);
        }

        /// <summary>
        /// 快递单
        /// </summary>
        /// <param name="type"></param>
        /// <param name="number"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public IQueryable<InvoiceView> ExpressOrderQuery(int type, string number, DateTime beginTime, DateTime endTime)
        {
            var result = from invo in dbcontext.Invoices.Where(a => a.Type == 2 && a.Status != -1)
                         join exp in dbcontext.ExpressOrders
                         on invo.Id equals exp.InvoicId
                         where invo.Number.Contains(number) && invo.ApplyTime <= endTime && invo.ApplyTime >= beginTime
                         select new InvoiceView
                         {
                             Id = invo.Id,
                             SubId = exp.Id,
                             CreatorName = invo.CreatorName,
                             ApplyTime = invo.ApplyTime,
                             Number = invo.Number,
                             TakeCompany = invo.TakeCompany,
                             ExpNumber = exp.ExpNumber,
                             ExpTypeName = exp.ExpTypeName
                         };
            return result;
        }


        public InvoiceView GetSingleInvoiceView(Guid subId)
        {
            var result = from invo in dbcontext.Invoices.Where(a => a.Type == 1)
                         join sendCar in dbcontext.SendCars
                         on invo.Id equals sendCar.InvoicId
                         where sendCar.Id == subId
                         select new InvoiceView
                         {
                             Id = invo.Id,
                             SubId = sendCar.Id,
                             Status = sendCar.Status,
                             CreatorName = invo.CreatorName,
                             ApplyTime = invo.ApplyTime,
                             DliveryDemand = sendCar.Demand,
                             Number = invo.Number,
                             Worth = sendCar.Worth,
                             TakeCompany = invo.TakeCompany,
                         };

            return result.FirstOrDefault();
        }

        public InvoiceView GetSingleInvoiceView1(Guid subId)
        {
            var result = from sendCar in dbcontext.SendCars.Where(a => a.Type == 1)
                          where sendCar.Id== subId
                         select new InvoiceView
                          {
                              Id = sendCar.Id,
                              SubId = sendCar.Id,
                              Status = sendCar.Status,
                              CreatorName = sendCar.CreatorName,
                              ApplyTime = sendCar.CreateTime.Value,
                              DliveryDemand = sendCar.Demand,
                              Number = sendCar.Number,
                              Worth = sendCar.Worth,
                              TakeCompany = sendCar.TakeCompany,
                              SendType = sendCar.SendType,
                              SendCardType = sendCar.Type
                          };

            return result.FirstOrDefault();
        }
    }
}
